package com.lagou.hive.udf;

import com.alibaba.fastjson.JSONObject;
import com.lagou.hive.utils.HttpUtils;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.joda.time.DateTime;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 查询日期分类
 * 0上班 1休息 2节假日
 */
public class ParseDayInfo extends UDF {

    public int evaluate(String dateStr) {
        Map<String, Object> param = new HashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            dateStr = new DateTime(simpleDateFormat.parse(dateStr)).toString("yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        param.put("d", dateStr);
        String s = HttpUtils.httpGet("http://www.easybots.cn/api/holiday.php", param);
        JSONObject jsonObject = JSONObject.parseObject(s);
        int dayInfo = jsonObject.getInteger(dateStr);
        return dayInfo;
    }

    @Test
    public void test() {
        //0上班 1休息 2节假日
        int dayTag = evaluate("2020-05-01 00:00:00");
        System.out.println("-------" + dayTag + "-------");
    }

}
